<?php
class Control_Shell_operation {
 	function operation() {
		
		$nowGame 	= $game = MooForm::request('game');
		$platform 	= MooForm::request('platform');
		$game 		= MooObj::get('Game')->transportGame($game);
		$args 		= MooForm::request('args');
		$serverids 	= MooForm::request('servers');
		$timeout 	= MooForm::request('timeout');
		$type 		= MooForm::request('type'); // exec 执行任务 否则 不执行
		
		// 默认60s
		!$timeout && $timeout = 60;
		
		$shelloperationGet 	= MooForm::request('shelloperation');
		
		$shelloperation = MooConfig::get('shelloperation');
		MooView::set('shelloperation', $shelloperation);
		
		$shells = "";
		foreach ($shelloperation as $key => $val) {
			if($shelloperationGet == $val['shell']) {
				$tips = $val['params'];
				if($game == "rxcq" || $game == 'qjgj') {
					if(!empty($val['params2'])) {
						$tips = $val['params2'];
					}
				}	
			}
			if($val['type'] == 1) {
				if($shells=="") {
					$shells = $val['shell'];
				} else {
					$shells .= "|".$val['shell'];
				}
			}
		}
		
		$tips = str_replace('game', $game, $tips);
		
		if($shelloperationGet == "update") {
			MooView::set('shells', $shells);
//		} else if($shelloperationGet == "syncserver.sh"  || $shelloperationGet == "serverscript.sh") {
		} else if($shelloperationGet == "syncserver.sh" || ($shelloperationGet == "serverscript.sh" && $args != "xmlcheck")) {

				MooView::set('tipsValue', $tips);
		}
		
		// 提示信息
		MooView::set('tips', $tips);
		
		if(!$game) {
			$timeout = 60;
			MooView::set('timeout', $timeout);
			MooView::render("shellOperation");
		} else {
			
			if($args) {
				$postData = $shelloperationGet . " --game " . $game . " --serverids " . $serverids . " --args " . $args;
			} else {
				$postData = $shelloperationGet . " --game " . $game . " --serverids " . $serverids;
			}
			
			// 超时时间 毫秒
			$postData =  $postData . " --timeout " . $timeout * 1000; 
			
			$postShellOps = MooConfig::get('main.postShellOps');
			!$postShellOps && $postShellOps = "http://123.59.64.163:9010/ops";
			
			// 记录log
			$urlLog  = $postShellOps . "?params=" . $postData;
			$date = date("Y-m-d");
			$log = ROOT_PATH . "/urlLogs/" . "Shell_operation.log." . $date;
			// 记录中文log
			$logUrl = urldecode($urlLog);
			MooFile::write($log, date('Y-m-d H:i:s') ."--" . $logUrl  ."\n", true);
			
			// 执行shell的判断
			if($shelloperationGet && $game && $serverids && $type == "exec") {
				$ret = MooUtil::curl_send($postShellOps, $postData, $timeout);
			}
		
//			$ret = '[{"identify":"qmby.test.1","result":"success"}]';
			
	 		if($ret) {
	 			$resArr = MooJson::decode($ret, true);
	 		}
	 		
	//		$fetchServerUrl = MooConfig::get('main.url.fetch_server');
			$fetchServerUrl = MooConfig::get('main.url.serverlist');
			$serverUrl  = $fetchServerUrl . "?game=" . $game;
			
			$readByConf = MooConfig::get('main.readByConf');
			// 走配置或者走接口
			if($readByConf == 1) {
				$serverInfoArr = MooConfig::get('shellservers.' . "{$game}");
				MooView::set('readByConf', $readByConf);
			} else {
				$serverInfo = MooUtil::curl_send($serverUrl);
				$serverInfoArr  =  MooJson::decode($serverInfo, true);
			}
			
			$servers = array();
			if($serverInfoArr) {
				foreach ($serverInfoArr as $key => $val) {
					if(!$platforms) {
						$platforms[] = $val['platform'];
					} elseif(!in_array($val['platform'], $platforms)) {
						$platforms[] = $val['platform'];
					}
					$arr['platform'] 		=  $val['platform'];
					$arr['serverId'] 		=  $val['serverId'];
					$arr['folder'] 			=  $val['workPath'];
					if ($platform) {
						if ($arr['platform'] == $platform) {
							$arr['name'] 		=  $val['platform'] .".". $val['serverId'];
							$arr['ip']	 		= $val['myip']?$val['myip']:"127.0.0.1";
							$arr['scriptPort']	= $val['scriptPort'];
							$arr['res']	 		= '';
							$arr['status']	 	= '';
							$arr['dburl']	 	= $val['dbUrl'];
							
							if ($arr['ip'] && $arr['scriptPort']) {
								$arr['type']	 	= 1;	// 有效
							} else {
								$arr['type']	 	= 0;	// 无效 
							}
							$arr['isView']  = 0;
							$servers[$arr['name']] = $arr;
						}
					} else {
						
						$arr['name'] 		=  $val['platform'] .".". $val['serverId'];
						$arr['ip']	 		= $val['myip'] ? $val['myip']:"127.0.0.1";
						$arr['scriptPort']	= $val['scriptPort'];
						$arr['res']	 		= '';
						$arr['status']	 	= '';
						$arr['dburl']	 	= $val['dbUrl'];
						
						if ($arr['ip'] && $arr['scriptPort']) {
							$arr['type']	 	= 1;	// 有效
						} else {
							$arr['type']	 	= 0;	// 无效 
						}
						$arr['isView']  = 0;			// 是否勾选
						
						$servers[$arr['name']] = $arr;
					} 
				}
			}
			
			// 将结果塞入数据
			if($resArr) {
				$resultArr = array();
				foreach($resArr as $k => $v) {
					$resultArr[$v['identify']] = $v['result'];
				} 
				
				foreach($servers as $key => $val) {
					$gameServer = $game.'.'.$key;
					if(!empty($resultArr[$gameServer])) {
						// 结果写入
						$servers[$key]['res'] 		= $resultArr[$gameServer];
						$servers[$key]['status']	= "已完成";
						$servers[$key]['isView']	= 1;	//勾选上
					} 
				}
			} else {
				if(!empty($serverids)) {
					$serveridArr = explode(",", $serverids);
					foreach($servers as $key => $val) {
						if(in_array($key, $serveridArr)) {
							// 结果写入
							$servers[$key]['res'] 		= "";
							$servers[$key]['status']	= "";
							$servers[$key]['isView']	= 1;	//勾选上
						}
					}
				}
			}
			
			if($servers) {
				$selectAll = "all";
				MooView::set('selectAll', $selectAll);
				$selectNo = "全不选";
				MooView::set('selectNo', $selectNo);
			}
			
			if ($platform) {
				MooView::set('platform', $platform);
			}
			
			MooView::set('nowTime', date('Y-m-d H:i:s'));
			MooView::set('Time', date('H:i:s'));
			
			MooView::set('shelloperationType', $shelloperationGet);
			MooView::set('timeout', $timeout);
			MooView::set('nowGame', $nowGame);
			
			MooArray::tdSort($servers, "platform", SORT_ASC, "serverId", SORT_ASC);
			
// 			MooFile::write('db.log', MooJson::encode($servers));
			
			MooView::set('servers', $servers);
			MooView::set('platforms', $platforms); 
			
			if($shelloperationGet != "syncserver.sh") {
				MooView::set('args', $args);
			}
		
			// 获得cmd
			$updateCmd = "sh -c /data/www/".$game."/build_xml.sh";
			$updateCmd = base64_encode($updateCmd);
			$updateCmd = str_replace("/","_",$updateCmd);
			$updateCmd = str_replace("=","-",$updateCmd);
			
			$url = "http://123.59.64.163:9080/shell?params=cmd:{$updateCmd}&user=hawk";
			MooView::set('updateUrl', $url);
			
			// 获得cmd
			$jarUpdateCmd = "sh -c /data/www/".$game."/build.sh";
			$jarUpdateCmd = base64_encode($jarUpdateCmd);
			$jarUpdateCmd = str_replace("/","_",$jarUpdateCmd);
			$jarUpdateCmd = str_replace("=","-",$jarUpdateCmd);
			
			$jarUrl = "http://123.59.64.163:9080/shell?params=cmd:{$jarUpdateCmd}&user=hawk";
			MooView::set('updateUrlJar', $jarUrl);
			MooView::render("shellOperation");
		}
	}
}
